package com.leo;

import javafx.collections.ObservableList;
import javafx.scene.control.SpinnerValueFactory;

/**
 * @ClassName MySpinnerValueFactory
 * @Description
 * @Author wangss
 * @date 2020.05.26 15:20
 * @Version 1.0
 */
public class MySpinnerValueFactory extends SpinnerValueFactory<Student> {

    private int index = -1;
    private ObservableList<Student> list;

    public MySpinnerValueFactory(ObservableList<Student> list) {
        this.list = list;
    }

    @Override
    public void decrement(int steps) {
        index = index - steps;
        if (index <= 0) {
            index = 0;
        }

        // 这里会触发 setConverter 中的toString方法
        this.setValue(list.get(index));
    }

    @Override
    public void increment(int steps) {
        index = index + steps;
        if (index >= list.size()) {
            index = 0;
        }

        this.setValue(list.get(index));
    }
}
